<?php
/**
 * +----------------------------------------------------------------------
 * | [ DO WHAT YOU LOVE AND FUCK THE REST ]
 * +----------------------------------------------------------------------
 * | EMAIL: < 931274989@qq.com >
 * +----------------------------------------------------------------------
 * | WECHAT: xzc016
 * +----------------------------------------------------------------------
 * | 模型基类
 * +----------------------------------------------------------------------
 */
namespace app\admin\model;

use think\facade\Session;
use think\Model;

class Base extends Model
{
  // 获取左侧主菜单
  public static function getMenus($authRule=[])
  {
    if(!$authRule) {
      $authRule = AuthRule::where('status', 1)
      ->where('auth_open', '=', 1)
      ->order('sort asc')
      ->select()
      ->toArray();
    }

    $menus = array();
    foreach ($authRule as $key => $val) {
      $authRule[$key]['href'] = url($val['name']);
      if ($val['pid'] == 0) {
        if (Session::get('admin.id') != 1) {
          if (in_array($val['id'], Session::get('admin.rules', []))) {
            $menus[] = $val;
          }
        } else {
          $menus[] = $val;
        }
      }
    }
    foreach ($menus as $k => $v) {
      $menus[$k]['_child'] = [];
      foreach ($authRule as $kk => $vv) {
        if ($v['id'] == $vv['pid']) {
          if (Session::get('admin.id') != 1) {
            if (in_array($vv['id'], Session::get('admin.rules'))) {
              $menus[$k]['_child'][] = $vv;
            }
          } else {
            $menus[$k]['_child'][] = $vv;
          }
        }
      }
    }
    return $menus;
  }
}